Skip to content

Reimplement 18 TribesState functions at 100% from #279, plus 5 with register allocation differences - #555

Open
gynt wants to merge 40 commits into
sourcehold:mainfrom
gynt:reimpl/pr279-matched
Open

gynt wants to merge 40 commits into
sourcehold:mainfrom
gynt:reimpl/pr279-matched

Conversation

@gynt

@gynt gynt commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

This PR takes the finished part of draft #279 (reimpl/TribesState). It has 23 TribesState functions:

  • 18 that reccmp puts at 100%. 16 are at raw 100%. The other 2 count as 100% under main's existing conventions, because only a label differs.
  • 5 where only register allocation differs, with no functional difference. Their status is <pct>% | Register allocation differs.

Measured on origin/main 55fa231 (merged into this branch), after the goto-free rewrite of playUnitSelectionSound and commandUnitsToLocation:

  • The build list held these 23 files plus main's 75 TribesState sources.
  • Both resolver sets were on: enable_local_functions.py and enable_reimplemented_data.py.
  • I ran a full build with build_parallel.py, then reccmp.
  • After the style pass I built and measured again. The numbers below are from that build.
  • The first 18 were first measured on f28b77b. Their numbers did not change on 592a17d, 0701799, 87e40b6 or 55fa231.
Function Address Raw % Normalized % Status
TribesState::addUnitToTribe 0x00522590 100.0 100.0 100.0% | Reimplemented
TribesState::createTribe 0x00522890 100.0 100.0 100.0% | Reimplemented
TribesState::createTribeForPlayer 0x005227E0 100.0 100.0 100.0% | Reimplemented
TribesState::getAvailableTribeID 0x00522720 100.0 100.0 100.0% | Reimplemented
TribesState::getUnitIDForIndexInTribe 0x00522390 100.0 100.0 100.0% | Reimplemented
TribesState::moveUnitToBehaviorTarget 0x00521280 100.0 100.0 100.0% | Reimplemented
TribesState::processDeerMoving 0x0052AD50 100.0 100.0 100.0% | Reimplemented
TribesState::removeUnitFromThisTribeIfInTribe 0x00522630 100.0 100.0 100.0% | Reimplemented
TribesState::removeUnitFromTribe 0x00525A70 100.0 100.0 100.0% | Reimplemented
TribesState::snapshotSelectionTribeAndComputeStance 0x00522CA0 100.0 100.0 100.0% | Reimplemented
TribesState::spawnDeerLionOrRabbit 0x00525F20 100.0 100.0 100.0% | Reimplemented
TribesState::trimTribeToSize 0x00522EF0 100.0 100.0 100.0% | Reimplemented
TribesState::updateCamelTribeBehavior 0x0052B630 100.0 100.0 100.0% | Reimplemented
TribesState::updateLionWolfTribeBehavior 0x0052B110 100.0 100.0 100.0% | Reimplemented
TribesState::updateRabbitTribeBehavior 0x0052B390 100.0 100.0 100.0% | Reimplemented
TribesState::upgradeTribeArrayLayoutForMapVersion 0x00523EF0 100.0 100.0 100.0% | Reimplemented
TribesState::playUnitMoveSpeech 0x00521EF0 99.4 100.0 100.0% | Reimplemented (counted: only the playSpeechSFX tail-jump target differs; it goes through main's trampoline)
TribesState::moveTribeToIndexedNearbyTile 0x0052A700 98.6 95.2 100.0% | Reimplemented (counted: only the loop-end symbol differs)
TribesState::playAttackCommandFeedback 0x00522090 98.0 94.4 98.0% | Register allocation differs
TribesState::createAnimal 0x00525BA0 96.5 71.4 96.5% | Register allocation differs
TribesState::playUnitCombatSpeechForTarget 0x00521A90 94.4 90.7 94.4% | Register allocation differs
TribesState::playUnitSelectionSound 0x005217E0 92.4 88.5 92.4% | Register allocation differs
TribesState::commandUnitsToLocation 0x00526F00 90.3 87.2 90.3% | Register allocation differs

What is left in the 5 register-allocation functions. I checked the full reccmp diff of each with registers normalized. Every remaining line is a register or slot choice, a load/reload order, a reg-reg copy, or an offset that shifts because of those. No store, computation or branch differs.

  • playAttackCommandFeedback: the tribe ID is loaded from its parameter slot into ebx before the selectionContainsCombatUnit call, not after it. Nothing writes that slot in between.
  • createAnimal: for the three spawn-point calls, x and y are reloaded from their parameter slots into other registers, and the reloads and pushes are ordered differently around the store to the tribe's herd size. The same values are pushed in the same order.
    • The review pass removed one more reload-order hunk: raw went from 96.1 to 96.5. reccmp's normalized figure fell from 77.6 to 71.4, but the diff is a strict subset of the earlier one.
  • playUnitCombatSpeechForTarget:
    • The unitID parameter is loaded once above the range chain and copied, where the original loads it twice at the merge.
    • The two unit-position loads swap order, along with the imul that indexes one of them.
    • The selected count is copied through eax to edi.
    • The remaining lines are jump offsets and one jump-table entry, shifted by those bytes.
  • playUnitSelectionSound: the selected count is loaded into eax and copied to edi, an un-coalesced move, where the original loads it straight into edi. The remaining lines are jump offsets, the jump table and the alignment filler shifted by those 2 bytes, plus tail-jump targets.
  • commandUnitsToLocation:
    • this and the index swap edi and ebp. reg_scores gives both the same score.
    • The original stores the 1 flag and spills the tribe offset before the empty-tribe test; ours does it around the test. That exit returns FALSE without reading either value.

Why the last two count as 100%:

  • playUnitMoveSpeech: the only difference is the target of the two tail jumps to SFXState::playSpeechSFX (0x0044A3A0). In our build they go through main's trampoline.
  • moveTribeToIndexedNearbyTile: the only differing line is the loop-end compare.
    • Both binaries compare against 0xb4df44, which is &field_0xcc4[40].y, 4 bytes past UnitSelectionDefinedData.
    • reccmp names that address after the next global in ours and leaves it unnamed in the original.
    • Main already counts the same case as 100% for 0x004DA200, 0x004DA300 and 0x004DAE00.

Enum headers (from main's 87e40b6, byte-identical to this PR's earlier enum commits):

Header changes: none; all headers are main's. Main's Ghidra sync (0701799) now declares moveTribeToIndexedNearbyTile as void, so TribesState.hpp and TribesState.func.hpp are main's again. The earlier commit that changed them is superseded by the merge.

Not taken from the draft:

  • The draft also adds a _memmove stub to src/OpenSHC/OS/OS.cpp. It is not needed here: OS.cpp is not in any build list, and upgradeTribeArrayLayoutForMapVersion links through main's OS_Func::_memmove resolver without it.
  • GL/visibility: none of these files needs an entry in cmake/compiler-flags-gl.txt or cmake/function-visibility.txt, and the draft has none either.

Style pass: every file follows REIMPLEMENTATION_STYLE_GUIDE.md as it is on main. Each change was measured with the full build above, and no function moved.

  • The class's own ../TribesState.func.hpp is the first include, and the unused Map.func.hpp include is gone.
  • Unused and redundant using declarations are dropped.
  • Self-operations replace x = x + 1 and similar.
  • countof(...) replaces the literals 1250, 1249 and 200.
  • if (flag) replaces != FALSE.
  • 0 replaces '\0' for a byte value.
  • Enum names replace the numbers 0x2f (UT_CAMELSHBEAR) and 0xd0 (US_UNKNOWN_208), and eSFX names replace the sound IDs in the two selection-sound functions. The remaining hex literals there are now decimal.
  • Parameters and locals have names (behaviorType, tribeID, unitType, candidateX, animalCount, spawnIndex, ...).
  • getUnitIDForIndexInTribe uses continue.
  • The do-while loops in moveTribeToIndexedNearbyTile, trimTribeToSize and createAnimal are now for loops.
  • Two changes cost match and were reverted. The PR comment below says why:
    • A countof loop to zero snapshotSelectionTribeAndComputeStance's three stance counters took it from 100% to 90.9% normalized. The three stores stay.
    • A for loop in commandUnitsToLocation loads the tribe size separately (movzx + test instead of a compare against memory). That is an instruction-selection difference, so the do-while stays.

Review pass (the review: commit), measured again with a full build:

  • Tribe states, map constants (MC_MAP_XY_LIMIT*, MC_MAP_SECTOR_SIZE), the Logic1 flags that make up createAnimal's terrain mask, UT_NONE, and the STBT_0x3f2… behaviour names replace the numbers.
  • More literals are decimal.
  • dying is tested for truthiness.
  • Code comments that explained forms kept for the match are gone. Their content, plus the header types and missing enums I left alone, is in the PR comment.

Overlap with open PRs:

There is one commit per function. Each one sets that function's line in status/addresses-SHC-3BB0A8C1.txt.

Draft #279 stays open for the 15 functions that are not done yet:

  • giveTribeMoveInstruction, giveTribeAnInstruction, spawnQueuedReinforcementWaves, computeTribePercentages, sortTribePathDestinationsByCost
  • getSpecificUnitFromTribe, isTribePathToDestinationClear, assignAttackTargetsForTribe, applyUnitTopSpeedDelayBasedOnTribeSize, updateTribeUnitAssignments
  • drawFlagsAndUnitDestinations, updateTribeCombatStanceBehavior, predictUnitInterceptPosition, updateTribes, isTribeUnitBlockedByOtherUnit

Each of those still has a difference that is more than register allocation:

  • updateTribes: a duplicated call block.
  • isTribeUnitBlockedByOtherUnit: a recomputed unitID * 0x490.
  • predictUnitInterceptPosition: lea vs add and a reordered computation.
  • applyUnitTopSpeedDelayBasedOnTribeSize: a different frame, a different load form and a block-layout jmp.
  • updateTribeUnitAssignments, updateTribeCombatStanceBehavior, getSpecificUnitFromTribe: compare selection, store and block differences.
  • The rest: larger structural differences.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp

gynt and others added 20 commits December 4, 2026 15:31
The function returns nothing: the value left in EAX is the quotient of the
idiv on the early exit, the y coordinate or the path test's 0 at the end of
the search, and its only caller (updateTribeCombatStanceBehavior, 0x52AC9A)
ignores it. The reimplementation in the next commits is `void`, and with
main's `int` declaration it does not compile (C2440 in the resolver), so the
declaration and the resolver type in TribesState.func.hpp change with it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::addUnitToTribe from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::createTribe from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::createTribeForPlayer from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::getAvailableTribeID from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::getUnitIDForIndexInTribe from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::moveUnitToBehaviorTarget from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::processDeerMoving from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::removeUnitFromThisTribeIfInTribe from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::removeUnitFromTribe from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::snapshotSelectionTribeAndComputeStance from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::spawnDeerLionOrRabbit from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::trimTribeToSize from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::updateCamelTribeBehavior from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::updateLionWolfTribeBehavior from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::updateRabbitTribeBehavior from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::upgradeTribeArrayLayoutForMapVersion from sourcehold#279. Measured at 100.0 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::playUnitMoveSpeech from sourcehold#279. Measured at 99.4 / normalized 100.0 on origin/main f28b77b (full build, both resolver sets on, reccmp). The only difference is the target of the two tail jumps to SFXState::playSpeechSFX (0x0044A3A0), which in ours go through main's trampoline; counted as 100% by main's convention.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::moveTribeToIndexedNearbyTile from sourcehold#279. Measured at 98.6 / normalized 95.2 on origin/main f28b77b (full build, both resolver sets on, reccmp). The one differing line is the loop-end compare: both binaries compare against 0xb4df44 (&field_0xcc4[40].y, 4 bytes past UnitSelectionDefinedData), which reccmp names after the next global in ours and leaves unnamed in the original. Counted as 100% by main's convention (as 0x004DA200, 0x004DA300, 0x004DAE00).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
sourcehold#554 carries TribesState::addUnitToTribe too, written `size++` where this branch had `++size`. Taking its exact text makes the two PRs add an identical file, so whichever merges second has no conflict on it. Still 100.0 / normalized 100.0 (full build and reccmp on f28b77b).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@github-actions github-actions Bot added the declaration/type change This requires changes to the generated/exported types or definitions. label Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

15 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 4 of the 18 reimplemented .cpp file(s) in this PR.

The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.

rule what it asks hits
18 Use the name, not the number 13
23 Prefer if-else over ternary 1
24 Declare variables upon first use 1
Where

Rule 18 — Use the name, not the number

  • src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp:44 — hex constant 0x3F2; use a name, or decimal
  • src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp:52 — hex constant 0x3F5; use a name, or decimal
  • src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp:60 — hex constant 0x3FB; use a name, or decimal
  • src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp:72 — hex constant 0x3F2; use a name, or decimal
  • src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp:78 — hex constant 0x3FD; use a name, or decimal
  • src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp:86 — hex constant 0x3F6; use a name, or decimal
  • src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp:94 — hex constant 0x413; use a name, or decimal
  • src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp:102 — hex constant 0x414; use a name, or decimal
  • src/OpenSHC/Map/Units/TribesState/moveUnitToBehaviorTarget.cpp:110 — hex constant 0x3F7; use a name, or decimal
  • src/OpenSHC/Map/Units/TribesState/upgradeTribeArrayLayoutForMapVersion.cpp:20 — hex constant 0x26C; use a name, or decimal
  • ... and 3 more

Rule 23 — Prefer if-else over ternary

  • src/OpenSHC/Map/Units/TribesState/spawnDeerLionOrRabbit.cpp:67 — ternary; the guide prefers extending the if-else chain

Rule 24 — Declare variables upon first use

  • src/OpenSHC/Map/Units/TribesState/createTribeForPlayer.cpp:25 — declare tribeID where it is first assigned

gynt and others added 6 commits December 4, 2026 15:58
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::createAnimal from sourcehold#279, with the style pass. Measured at 96.1 / normalized 77.6 on origin/main 592a17d (full build, both resolver sets on, reccmp). Remaining: the reloads of x and y from their parameter slots for the three spawn-point calls, and their pushes, use other registers and are ordered around the store to herdMaxSize; the same values are pushed in the same order. No functional difference.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::playAttackCommandFeedback from sourcehold#279, with the style pass. Measured at 98.0 / normalized 94.4 on origin/main 592a17d (full build, both resolver sets on, reccmp). Remaining: the tribe ID is loaded from its parameter slot into ebx before the selectionContainsCombatUnit call instead of after it; nothing writes that slot in between. No functional difference.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::commandUnitsToLocation from sourcehold#279, with the style pass. Measured at 90.3 / normalized 87.2 on origin/main 592a17d (full build, both resolver sets on, reccmp). Remaining: this and index swap edi and ebp (a reg_scores tie), and the original stores the 1 flag and spills the tribe offset before the empty-tribe test, ours around it; that exit returns FALSE without reading either. No functional difference.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::playUnitSelectionSound from sourcehold#279, with the style pass. Measured at 92.4 / normalized 88.5 on origin/main 592a17d (full build, both resolver sets on, reccmp). Remaining: the selected count is loaded into eax and copied to edi (an un-coalesced move) where the original loads it straight into edi; the other lines are jump offsets, the jump table and the alignment filler shifted by those 2 bytes, and tail-jump targets. No functional difference.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
TribesState::playUnitCombatSpeechForTarget from sourcehold#279, with the style pass. Measured at 94.4 / normalized 90.7 on origin/main 592a17d (full build, both resolver sets on, reccmp). Remaining: the unitID parameter is loaded once above the range chain and copied, where the original loads it twice at the merge; the two unit-position loads (and the imul that indexes one) swap order; the selected count is copied through eax to edi; the rest are jump offsets and one jump-table entry shifted by those bytes. No functional difference.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@gynt gynt changed the title Reimplement 18 TribesState functions at 100% from #279 Reimplement 18 TribesState functions at 100% from #279, plus 5 with register allocation differences Dec 4, 2026
Tribe::tribeState is a plain short holding 0, 2 or 3: 0 marks a free slot (createTribe, createTribeForPlayer and getAvailableTribeID search for it), 2 an active tribe (set when a tribe is created, required by getUnitIDForIndexInTribe), and 3 a tribe whose last unit was removed (removeUnitFromTribe, removeUnitFromThisTribeIfInTribe). The TribesState functions in this PR compare and assign these values by name. Tribe.hpp keeps its short field; it can become TribeStateShort upstream.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
gynt and others added 2 commits December 4, 2026 17:01
The 16-bit alias of TribeState, following the Enums/<Name>Short.hpp convention, for Tribe::tribeState (0x18, a short) once the generated header takes it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
- Tribe states by name (Enums::TS_FREE / TS_ACTIVE / TS_EMPTY) instead of 0/2/3.
- Map constants: MC_MAP_XY_LIMIT_INCLUSIVE / MC_MAP_XY_LIMIT for the 399 and
  400 tile bounds, MC_MAP_SECTOR_SIZE for the wildlife grid's x / 10, y / 10.
- createAnimal: the terrain mask 0x4a5014b1 spelled out as its eleven Logic1
  flags, L_SEA for the seagull test, UT_NONE for the initial unit type.
- moveUnitToBehaviorTarget: SomeTribeBehaviorType names (STBT_0x3f2 ...) for
  the behaviour cases, BOOLEnum for the found flag.
- Decimal instead of hex for speech action IDs, the fade countdown, the map
  version and the old record sizes.
- `dying` tested for truthiness.
- Comments that explained forms kept for the match are removed; their content
  goes into a PR comment. The old-layout note in
  upgradeTribeArrayLayoutForMapVersion stays, and trimTribeToSize gets a
  fixme: for the original ignoring its tribeID parameter.

Re-measured with a full build and reccmp, both resolver sets on, on main
592a17d: every 100% function stays at 100%. createAnimal goes from 96.1 to
96.5 raw (one fewer reload-order hunk; the rest is unchanged), and the other
four register-allocation functions are unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@gynt

gynt commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

These forms are kept because the match needs them. The explanations used to be code comments and are collected here instead.

Tribe access through a shifted TribesState* (spawnDeerLionOrRabbit, trimTribeToSize)

  • (TribesState*)((char*)this + tribeID * sizeof(Tribe)) and then ->tribes[0].field, instead of this->tribes[tribeID].field.
  • The original takes lea reg, [eax + ecx] and reads every field at its offset inside TribesState. A Tribe& or plain indexing folds the tribes offset into the base, which measured 0.93 against 1.0 in quick_diff.

Separate loads and guards

  • createTribe, createTribeForPlayer, getAvailableTribeID: the search loop breaks out, and the result is tested again with tribeID <= 0 after the loop. The original jump-threads that test into the loop's exhausted exit but keeps it on the found path.
    • In getAvailableTribeID, the solitary search counts DAT_CurrentTribeID itself. Counting a local beside it stores the global from a register copy.
  • getUnitIDForIndexInTribe: two separate early returns. The original pushes ebx/esi/ebp only after the tribe-state test; a combined state == TS_ACTIVE && index < size does not do that.
  • createAnimal: the three x, y, BinaryTileMap guards stay separate ifs, because the original gives each one its own forward jump.

Locals and their order

  • moveUnitToBehaviorTarget: each arm stores y, x, tile and the found flag itself, and tribeID is read after those four locals are declared. With one shared assignment after the arms, unitID took ebx: 37.1%.
  • removeUnitFromTribe, removeUnitFromThisTribeIfInTribe: wordIndex and bitIndex get their own locals before the &=, because the original divides before it multiplies the tribe ID.
  • processDeerMoving, updateRabbitTribeBehavior: the wildlife grid cell is indexed in full at each read. Named x / MC_MAP_SECTOR_SIZE / y / ... locals move this and the tribe ID to other registers. In processDeerMoving the reads are ordered rocky tiles, lion count, marsh tiles.
  • createAnimal: the herd size stays a short (widening it costs 0.4 points). The function returns tribeID once at the end; a return per arm moved the spawn counter out of ebx (quick_diff 0.57 against 0.93).
  • snapshotSelectionTribeAndComputeStance: the three stance counters are zeroed with three stores. A countof loop drops the function from 100% to 90.9% normalized.
  • playAttackCommandFeedback: the parameter's home slot is reused as the selected count that getMajoritySelectedUnitType writes back, which is what the original does.

Control flow kept for tail calls

  • playUnitMoveSpeech: the selected count lives in a {} scope that ends before the switch. That lets the two out-of-ammunition plays compile to tail calls (jmp playSpeechSFX). Passing &tribeID instead keeps an address-taken variable alive and blocks the tail call.
  • playUnitSelectionSound: the selected count is address-taken, and MSVC does not tail-call while such a local is in scope. The siege-engine and engineer arms end in tail calls in the original, so they sit after the scope. The switch sets an isSiege flag and breaks, and an if (isSiege) block plus the engineer code follow the scope. This gives the same code as the earlier goto form; putting either arm inside the switch scored lower (quick_diff 46.2–49.0% against 49.8%).
  • commandUnitsToLocation:
    • The empty tribe and the loop sit in an if/else that both set noneCommanded = TRUE; the loop runs in the else branch, and one if (noneCommanded) return FALSE; follows. This replaces the earlier goto to the shared return FALSE at the same score (90.3 / 87.2). Setting the flag once, before the size test (in its declaration or as a statement), put the tribe offset in ebx and turned every 1 into an immediate (quick_diff 86.0% against 90.2%). Every flag form that ties sets TRUE in both branches; the branch order and != FALSE do not matter. A do { … } while (0) block with break also ties. Separate empty / hasUnits flags (BOOLEnum, int or bool, initialised or set in both branches) score 84.6%, an early return FALSE for the empty tribe 84.5%, a while loop 86.0%, and a result flag 80.7%.
    • The loop stays a do-while. A for (; index < size;) loads the size with movzx + test instead of comparing the field in memory (87.2% → 84.0% normalized).

Literal and cast forms

  • moveTribeToIndexedNearbyTile: candidateCount is a variable holding 40, not a literal. With % 40, MSVC emits a reciprocal multiply; the original does mov ecx, 0x28; idiv ecx.
  • upgradeTribeArrayLayoutForMapVersion:
    • The bitmask's second half is cleared with a loop, which MSVC 2005 turns into rep stosd like the original. memset(p, 0, 200) becomes a call.
    • The two copies go through OS_Func::_memmove, the game's own memmove at 0x583760.

Casts kept for the match (the review: drop redundant casts commit removed every cast whose removal leaves the code identical)

  • addUnitToTribe, removeUnitFromTribe, removeUnitFromThisTribeIfInTribe: (int)unitID / 16 and % 16. The header declares uint unitID, but the original divides signed (cdq; and edx, 0xf; add eax, edx; sar eax, 4). Without the cast MSVC emits an unsigned shift. Goes away once unitID is int in Ghidra (proposed).
  • commandUnitsToLocation: (char)matchUnitSpeeds. The original tests only the low byte (cmp byte ptr [esp + 0x24], 0); without the cast it tests the whole dword. Goes away once the parameter is char in Ghidra (proposed).
  • processDeerMoving, updateRabbitTribeBehavior: (short)predatorNearby, and spawnDeerLionOrRabbit: (short)field_0x284. The original compares both signed (cmp ax, 0x320; jl, movsx edx, dx; cmp edx, eax; jl); the fields are ushort. Goes away once both fields are short in Ghidra (proposed; the same code with the field changed and the cast dropped).
  • processDeerMoving, updateCamelTribeBehavior, updateRabbitTribeBehavior: (short)unknownAttackRelatedUpdateCounter. These compare it signed, but TroopValueState::updateTribeBehaviorBasedOnBehaviorType (0x0051D7F0) reads the same field with movzx, so ushort stays and so do these casts.
  • spawnDeerLionOrRabbit, trimTribeToSize: (char*)this + tribeID * sizeof(Tribe), the byte-offset form of the shifted TribesState* above.
  • upgradeTribeArrayLayoutForMapVersion: (char*)this->tribes and one (void*), needed to compile the byte-offset record pointer and its comparison with a Tribe*.
  • Enum casts on unitType arguments ((UnitType)unitType in playUnitCombatSpeechForTarget) are needed to compile: the field is a short.

Header types I left alone (BRIEF rule 1: main's headers lead unless the match needs a change)

  • trimTribeToSize(undefined4 tribeID, ...): the original ignores tribeID and uses DAT_CurrentTribeID. This is marked fixme: in the code.
  • playUnitMoveSpeech(undefined4) is a tribe ID.
  • moveUnitToBehaviorTarget(int, int param_2): the second parameter is a SomeTribeBehaviorType.
  • Tribe::tribeState could become Map::Enums::TribeStateShort.

No enum yet

  • The playUnitSpeech action IDs (15–30), the playUnitSpeechEffect IDs (11–14, 36), AITribeType 0 and 16, SomeTribeBehaviorType 0, and UnitLogicState 1 have no named values on main. Adding them means changing generated headers, so they stay as literals for now.

Map/Trees/TreeState.hpp already uses TS_ (TS_UNUSED, TS_SPAWNED, TS_ALIVE, TS_TO_BE_REMOVED, TS_STATIC). The tribe states become TRS_FREE, TRS_ACTIVE and TRS_EMPTY, in the enum and in every use. A rename only; quick_diff output for createTribe and removeUnitFromTribe is unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
gynt added a commit to gynt/OpenSHC that referenced this pull request Dec 4, 2026
TreeState.hpp/TreeStateShort.hpp become TreeStateEnum.hpp/TreeStateEnumShort.hpp, the types TreeStateEnum/TreeStateEnumShort, and the values TSE_UNUSED, TSE_SPAWNED, TSE_ALIVE, TSE_TO_BE_REMOVED and TSE_STATIC. Tree.hpp's state field and updateTreeStage follow. This frees the TS_ prefix for the TribeState enum in sourcehold#555 (TS_FREE, TS_ACTIVE, TS_EMPTY). A rename only: updateTreeStage compiles and its quick_diff output is unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Main's TreeState enum is renamed to TreeStateEnum with TSE_ values in sourcehold#549, which frees TS_ again, so the tribe states return to TS_FREE, TS_ACTIVE and TS_EMPTY. A rename only; quick_diff output for createTribe and removeUnitFromTribe is unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
gynt added a commit to gynt/OpenSHC that referenced this pull request Dec 4, 2026
This aligns the enum with sourcehold#555, which carries the same two files
byte for byte, so the two PRs do not conflict. TS_ becomes free once
sourcehold#549 renames TreeState to TreeStateEnum (TSE_).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
This aligns the enum with sourcehold#554, which carries the same two files
byte for byte, so the two PRs do not conflict:
- Map/Enums is main's folder for these headers; Map/Units/Enums does
  not exist on main.
- TS_FREE becomes TS_UNUSED and TS_EMPTY becomes TS_DISBANDED.
- TS_ is free once sourcehold#549 renames TreeState to TreeStateEnum (TSE_).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Main's upstream Ghidra sync (0701799) carries moveTribeToIndexedNearbyTile's
void return; TribesState.hpp and TribesState.func.hpp are taken from main.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
gynt added a commit to gynt/OpenSHC that referenced this pull request Dec 4, 2026
These files are byte-identical to the ones in sourcehold#554 and sourcehold#555:
TS_UNUSED = 0, TS_ACTIVE = 2, TS_DISBANDED = 3. The TS_ prefix relies
on sourcehold#549 renaming TreeState's values to TSE_.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
gynt added a commit that referenced this pull request Dec 4, 2026
Main's c1b50c4 renames the TreeStateEnum values to TSE_, and 87e40b6 adds
Map/Enums/TribeState.hpp and TribeStateShort.hpp, byte-identical to this
branch's copies.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@github-actions github-actions Bot removed the declaration/type change This requires changes to the generated/exported types or definitions. label Dec 4, 2026
gynt and others added 6 commits December 4, 2026 22:30
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
The switch sets isSiege for the siege engines and the engineer and breaks;
the siege block and the engineer block follow it as if/fall-through. Same
code as the goto form.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
The empty-tribe check and the none-commanded check both break out to the
shared return FALSE. Same score as the goto form.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…e(0)

The empty tribe sets noneCommanded in its own branch, and the loop runs in
the else branch; one test of the flag then returns FALSE. Same score as the
do/while(0) and goto forms.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Every removed cast was checked one at a time: the file's /FA listing is
identical without it and no new warning appears. createAnimal keeps the
createTribe result in an int instead of casting a dword local.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant